#include "client.h"


//执行指令
int execute_choose(int choose,struct login_status* stat,int* status,struct client_info* info,int socket_fd)
{
    switch(choose)
    {
    case 0:
        *status=1;
        break;
    case 1:
        {
            printf("请登录>>>>>\n");
            int size=0;
            //登录逻辑
            printf("请输入账号:");
            scanf("%s",stat->name);
            getchar();
            printf("请输入密码:");
            scanf("%s",stat->password);
            getchar();
            //合并初始地址
            sprintf(stat->path,"%s",stat->name);

            info->command=LOGIN;

            //拼接protocol

            //验证账号
            send(socket_fd,&info->command,sizeof(int),MSG_NOSIGNAL);
            size=strlen(stat->name);
            int ret= send(socket_fd,&size,sizeof(int),MSG_NOSIGNAL);
            if(ret ==-1)
            {
                connect_serve("127.0.0.1","8080",&socket_fd);
                send(socket_fd,&info->command,sizeof(int),MSG_NOSIGNAL);
                send(socket_fd,&size,sizeof(int),MSG_NOSIGNAL);
            }
            send(socket_fd,stat->name,size,MSG_NOSIGNAL);
            send(socket_fd,stat->password,strlen(stat->password),MSG_NOSIGNAL);
            //todo验证账号的正确并接受返回值修改本地结构体参数
            int stats=-1;
            recv(socket_fd,&stats,sizeof(int),MSG_WAITALL);
            if(stats==1)
            {
                stat->login=true;
                printf("%s->登录成功\n",stat->path);
                INFO_LOG("用户：%s于%s登录\n",stat->name,get_current_time());
            }
            else
            {
                memset(&stat,0,sizeof(stat));
                printf("用户名或者密码输入错误，请重新输入。\n");
                return 0;
                //       INFO_LOG("用户：%s于%s数据输入错误\n",stat->name,get_current_time());
            }
            break;
        }
    case 2:
        {
            printf("请注册>>>>>\n");
            //注册
            printf("请输入账号，小于8位:  ");
            char name[NAMELENTH];
            scanf("%s",name);
            getchar();
            printf("请输入密码,小于18位:  ");
            char password[PASSWORDLENGTH];
            scanf("%s",password);
            getchar();
            info->command=REGIST;
            //注册完成之后向服务器发送完成当前注册
            //判断用户名是否重复
            send(socket_fd,&info->command,sizeof(int),MSG_NOSIGNAL);
            int size=0;
            size=strlen(name);
            //char salt[SALT_LENGTH]={0};
            //int ret=recv(socket_fd,salt,sizeof(salt),MSG_WAITALL);
            //if(ret ==0)
            //{
            //    ERROR_LOG("服务器断开链接\n");
            //}
            //char hash[40]={0};
            // hash_password_with_salt(password,salt,hash);
            send(socket_fd,&size,sizeof(int),MSG_NOSIGNAL);
            send(socket_fd,name,size,MSG_NOSIGNAL);

            //发送密码哈希值
            send(socket_fd,password,strlen(password),MSG_NOSIGNAL);
            int stats=-1;
            recv(socket_fd,&stats,sizeof(int),MSG_WAITALL);
            if(stats==1)
            {
                printf("注册成功\n");
                INFO_LOG("用户：%s于%s成功注册账号\n",stat->name,get_current_time());
            }
            else 
            {
                printf("注册失败\n");
                INFO_LOG("用户：%s于%s注册账号失败\n",stat->name,get_current_time());
                return 0;
            }
            break;
        }
    default:
        printf("你的输入有问题,请检查后重新输入\n");
        break;
    }
    return 0;
}

//分割命令
int depart_command(struct client_info* info ,char * msg)
{
    char* command=strtok(msg," ");
    //命令匹配
    ensure_command(command,info);

    if(info->command!=ERR)
    {
        //跳过空白
        char* param=strtok(NULL," ");
        if(param!=NULL)
        {
            //把参数线拷贝到info
            memcpy(info->protocol,param,strlen(param));
        }
    }
    return 0;
}




